package conf

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"strings"
	"test_singo/utils"
)

var Directory *map[interface{}]interface{}

func LocalesInit(path string)  {
	file, err := ioutil.ReadFile(path)
	if err != nil {
		utils.Log().Panic("yaml文件打开报错", err)
	}
	m := make(map[interface{}]interface{})
	err = yaml.Unmarshal(file, &m)
	if err != nil {
		utils.Log().Panic("yaml文件解析报错", err)
	}

	Directory = &m
}

func Translate(format string) string {
	dic := *Directory
	keys := strings.Split(format, ".")

	for index, path := range keys {
		// 找到最后一层
		if len(keys) == index+1 {
			for k, v := range dic {
				if key, ok := k.(string); ok {
					if key == path {
						if value, ok := v.(string); ok {
							return value
						}
					}
				}
			}
			return path
		}
		// 如果还有下一层
		// todo

	}
	return ""
}
